#include <iostream>
#include <fmt/core.h>

using namespace std;
using namespace fmt;

typedef int Rank;
#define DEFAULT_CAPACITY 3

using namespace std;

template <typename T> class Vector {
    protected:
        Rank _size; int _capacity; T* _elem;
        void copyFrom(T const* A, Rank lo, Rank hi);
    public:
        // 构造函数
        Vector(int c = DEFAULT_CAPACITY, int s = 0, T v = 0)
        {_elem = new T[_capacity = c]; for (_size = 0; _size < s; _elem[_size++] = v);}
        Vector(T const* A, Rank n) {copyFrom(A, 0, n);}
        // 只读访问接口
        void surf() const {for (Rank i = 0; i < _size; i++) cout << format("{} ", _elem[i]);cout << endl;}
        Rank size() const {return _size;}
        // 可写访问接口
        Vector<T> & operator= ( Vector<T> const& ); //重载赋值操作符，以便直接克隆向量
        // 遍历
        // void traverse(void (*)(T&));
};

int main()
{
    Vector<double> example_vector(5, 3, 1);
    example_vector.surf();

    double x[4] = {4.54, 5.48, 48.02, 452.054};
    Vector<double> example_vector2(x, 4);
    example_vector2.surf();

    Vector<double> example_vector3 = example_vector;
    example_vector3.surf();
    example_vector3 = example_vector2;
    example_vector3.surf();

    return 0;
}

template <typename T>
void Vector<T>::copyFrom(T const* A, Rank lo, Rank hi) {
    _elem = new T[_capacity= 2 * (hi - lo)];
    _size = 0;
    while (lo < hi) 
        _elem[_size++] = A[lo++];
}

template <typename T> 
Vector<T>& Vector<T>::operator= (Vector<T> const& V) {         // return type Vector<T>&
    if (_elem) delete [] _elem;
    copyFrom(V._elem, 0, V.size());
    return *this;
}
